package trong.gold.soft.com.view;

import trong.gold.soft.com.main.mTetrisMidlet;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

import trong.gold.soft.com.model.Resources;
import trong.gold.soft.com.model.createImg;
import trong.gold.soft.com.model.mediaGame;
import trong.gold.soft.com.model.font.VietnameseFont;
import trong.gold.soft.com.view.game.TetrisClassic;

public class settingTetris extends Canvas{
	private String ncd = "Cài đặt";
	private String bat="Đang bật",tat="Đang tắt";
	private String nsoundName = "noSound.png",soundName="sound.png";
	private Image ktin,backImage,soundImg,nosoundImg,chonImg;
	private int latx=0,laty=0;
	private mTetrisMidlet midlet;
	public settingTetris(mTetrisMidlet midlet){
		this.midlet = midlet;
//		backgrImage = createImg.createImage("/com/image/" + Resources.backgrName);
		ktin = Resources.imgoto;
		backImage = Resources.backImage;
		chonImg = Resources.chonImg;
		soundImg = createImg.createImage("/com/image/process/" + soundName);
		nosoundImg = createImg.createImage("/com/image/process/" + nsoundName);
		
	}
	protected void paint(Graphics g) {
		setFullScreenMode(true);
		for(int i=0;i<=this.getWidth()/44;i++){
			g.drawImage(Resources.backgr, i*Resources.backgr.getWidth(), 0, Resources.TOP_LEFT_ANCHOR);
		}
		g.drawImage(Resources.imgicon, this.getWidth()/2 - Resources.imgicon.getWidth()/2, 10, Resources.TOP_LEFT_ANCHOR);
		g.setColor(0xd8fbb6);
		g.fillRect(0, this.getHeight() - 25, this.getWidth(), 25);
		g.drawImage(ktin, this.getWidth()/2 - ktin.getWidth()/2, this.getHeight()/2 - ktin.getHeight()/2, Resources.TOP_LEFT_ANCHOR);
		g.drawImage(backImage, this.getWidth() - 5, this.getHeight(), Graphics.BOTTOM | Graphics.RIGHT);
		g.drawImage(chonImg, 5, this.getHeight(), Graphics.BOTTOM | Graphics.LEFT);
		VietnameseFont.BlackBold.DrawString(g, ncd,this.getWidth()/2,this.getHeight()/2 - ktin.getHeight()/2 + VietnameseFont.BlackBold.getHeight(), Graphics.HCENTER);
		if(getsound()){
			g.drawImage(soundImg, this.getWidth()/2 - ktin.getWidth()/2 + soundImg.getWidth(), this.getHeight()/2 - soundImg.getHeight(), Resources.TOP_LEFT_ANCHOR);
			VietnameseFont.BlackBold.DrawString(g, bat,this.getWidth()/2 - ktin.getWidth()/2 + 2*soundImg.getWidth() + 5 + soundImg.getWidth()*2,this.getHeight()/2 + soundImg.getHeight()/3 - soundImg.getHeight(), Graphics.HCENTER);
		}else{
			g.drawImage(nosoundImg, this.getWidth()/2 - ktin.getWidth()/2 + soundImg.getWidth(), this.getHeight()/2 - soundImg.getHeight(), Resources.TOP_LEFT_ANCHOR);
			VietnameseFont.BlackBold.DrawString(g, tat,this.getWidth()/2 - ktin.getWidth()/2 + 2*soundImg.getWidth() + 5 + nosoundImg.getWidth()*2,this.getHeight()/2 + nosoundImg.getHeight()/3 - soundImg.getHeight(), Graphics.HCENTER);
			
		}
			
	}
	private boolean getsound(){
		return slSound.getsound();
	}
	private boolean setsound(){
		return slSound.setsound();
	}
	private mediaGame getmedia(){
		return slSound.getmedia();
		
	}
	private mediaGame setmedia(){
		return slSound.setmedia(true, "");
		
	}
	public void pointerPressed(int x,int y){
		latx=x;
		laty=y;
		for(int i=0;i<chonImg.getWidth();i++)
			for(int j=this.getHeight() - chonImg.getHeight();j<this.getHeight();j++){
				if(i==latx && j==laty){
					switchState(true);
					break;
				}
			}
		for(int i=this.getWidth() - nosoundImg.getWidth();i<this.getWidth();i++)
			for(int j=this.getHeight() - chonImg.getHeight();j<this.getHeight();j++){
				if(i==latx && j==laty){
					switchState(false);
					break;
				}
			}
	}
	private void switchState(boolean c){
		if(c){
			if(getsound()){
				setsound();
				repaint();
				if(getmedia()!=null)
					getmedia().stopMedia();
			}else{
				setsound();
				repaint();
				if(getmedia()!=null)
					getmedia().startMedia();
				else{
					
				}
			}
		}else{
			setnull();
			Runtime.getRuntime().gc();
			midlet.switchState(this,new menuTetris(midlet));
		}
	}
	public void keyPressed(int keyCode) {
		
		switch (keyCode) {
		case -7:
			switchState(false);
			break;
		case -6:
			switchState(true);
			break;
		}
	}
	private void setnull(){

		nsoundName=null;
		soundName=null;
		ktin=null;

		backImage=null;
		soundImg=null;
		nosoundImg=null;
	}
}
